package com.tomcat.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: ServletSample
 *
 */
 public class ServletSample extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
//   MyHbase myHbase=null;
//   SimpleQueryProcessingManager simpleQuery = null;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ServletSample() {		
		super();
		
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
//			out.println(ServletUtilities.DOCTYPE);//println doctype
//			out.println(ServletUtilities.headWithTitle("Search result"));//title
			
			out.println("<BODY >");
			
			
//	        String s=myHbase.getInfoTest(33);//"Hello, sanglei's test!";
//	        out.println(s);
//	        
//	        s = simpleQuery.searchFromIndex("shape image segmentation", 50);
//	        out.println(s);
	        
//	        SimpleAnswer[] simpleAnswer = simpleQuery.search("simon query xml ", 50);
	        
//	        printTable(out,simpleAnswer,50);
	        
	        out.println("</BODY></HTML>");
	        out.close();
	        
	       
	        
//	        request.setAttribute("result", simpleAnswer);
//	        request.getRequestDispatcher("index.jsp").forward(request,response);

	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
//		myHbase = MyHbase.getInstance();
//		simpleQuery = SimpleQueryProcessingManager.getInstance();
//		simpleQuery.init(this.getServletContext().getRealPath("graph/edge_big.txt"),198462);
		
	} 	  	    

//	public void printTable(PrintWriter out, SimpleAnswer[] sa,int k){
//		if (sa != null) {
//			double sum = 0.0d;
//			int i = 0;
//			int pathSum = 0;
//			int maxPath = 0 , tmp = 0, maxPathIndex = -1;
//			for (; i < sa.length; i++) {
//				
//				SimpleAnswer at = sa[i];
//				if (at == null) {
//					break;
//				}
//				out.println("<h3>Rank " + (i + 1) + ": {" + at.getScore() + "}</h3><br> ");
//				out.println("root[" + myHbase.getInfoTest(at.getRoot()) + "] <br>");
//				sum += at.getScore();
//				for (int j = 0; j < at.getLeafNumber(); j++) {
//					int[] path = at.getPath(j);
//					out.println("           ( ");
//					if (path != null) {
//						for (int m = 0; m < path.length; m++) {
//							out.println(myHbase.getInfoTest(path[m] )+ "<br>         , ");
//						}
//					}
//					out.println(" )<br>");
//				}
//				out.println("<br>");
//			}
//			if (i == 0) {
//				out.println("Sorry, no result is found.");
//			}
//			else {
//				out.println( "Average score: " + (sum / Math.min(k, i)));
//			}
//			
////			System.out.println("Total time exhausted: " + (endTime.getTime() - startTime.getTime()) + " ms");
//		}
//	 }
	}